#include"snake.h"

void CreateMenuWindow();//0

void CreatePlayWindow();//1

void CreateInstructionWindow();//2

void CreateEndWindow();//3

int CreatePauseWindow();//CreatePlayWindow函数中调用

void PrintSNAKE(int x,int y);

void gotoxy( int x, int y){
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void clrscr() {
    system("cls");
}

int ReadMaxScore(){
	FILE *fp;
    //打开文件
    fp = fopen("maxscore.txt", "r");
    if (fp == NULL) {
        maxScore = 0;
        //如果文件不存在，创建文件并写入初始值
        fp = fopen("maxscore.txt", "w");
        if (fp != NULL) {
            fprintf(fp, "%d", maxScore);
            fclose(fp);
        }
        return maxScore;
    }
    //读取最高分
    fscanf(fp, "%d", &maxScore);
    fclose(fp);
    return maxScore;
}

//Index窗口索引，判断当前应该打开哪个窗口
int Index = 0;

int main(){
	//窗口函数集成
    void (*p[])() = {
        CreateMenuWindow,
        CreatePlayWindow,
        CreateInstructionWindow,
        CreateEndWindow
    };
    //判断窗口
    while ( Index>=0 && Index<=3 ){
        //调用窗口
        (*p[Index])();
        //窗口内修改Index的值
    }
	return 0;
}

//在（x，y）处绘制SNAKE字体 
void PrintSNAKE(int x,int y){
	//定义"SNAKE"每个字的8x7点阵数据
	const char* S[] = {
    " ###### ",
    "#      #",
    "#       ",
    " ###### ",
    "       #",
    "#      #",
    " ###### "
	};

	const char* N[] = {
    "##     #",
    "# #    #",
    "#  #   #",
    "#   #  #",
    "#    # #",
    "#     ##",
    "#      #"
	};

	const char* A[] = {
    "   ##   ",
    "  #  #  ",
    "  #  #  ",
    " ###### ",
    " #    # ",
    "#      #",
    "#      #"
	};

	const char* K[] = {
    "#      #",
    "#    #  ",
    "#  #    ",
    "# #     ",
    "#  #    ",
    "#    #  ",
    "#      #"
	};

	const char* E[] = {
    "########",
    "#       ",
    "#       ",
    "########",
    "#       ",
    "#       ",
    "########"
	};
	// 在（x，y）处输出"贪吃蛇"艺术字 
	int i;
    for( i=0;i<7;i++) {
    	gotoxy (x,y+i);
        printf("%s  %s  %s  %s  %s", S[i], N[i], A[i],K[i],E[i]);
    }
}

void CreateMenuWindow (){
		//屏幕初始化
		//清屏
		clrscr();
		//获取屏幕缓冲区信息
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
   		GetConsoleScreenBufferInfo(hConsole, &csbi);
   		//获取控制台宽度、高度
  	  	int consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  	  	int consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
   		//绘制边框，大小为控制台长、宽的4/5，偏移量为1/10
		int padding_width = consoleWidth/10;
		int padding_height = consoleHeight/10;
		//绘制窗口边界
		int i;
		//up
		gotoxy(padding_width,padding_height);
		for (i=padding_width;i<consoleWidth-padding_width;i++){
			putchar('*');
		}
		//down 
		gotoxy(padding_width,consoleHeight-padding_height);
		for (i=padding_width;i<consoleWidth-padding_width;i++){
			putchar('*');
		}
		//left
		for (i=padding_height;i<consoleHeight-padding_height;i++){
			gotoxy(padding_width,i);
			putchar('*');
		}
		//right
		for (i=padding_height;i<consoleHeight-padding_height;i++){
			gotoxy(consoleWidth-padding_width,i);
			putchar('*');
		}
		//绘制左上角信息
		//最高分 
		gotoxy(0,0);
		printf ("历史最高分：%d",ReadMaxScore() );
		//移动操作,F5刷新
		gotoxy(0,1);
		printf ("WASD移动\tF5刷新\n");
		//绘制SNAKE艺术字
		int padding_SNAKE_x = consoleWidth/2-24;
		int padding_SNAKE_y = consoleHeight/5;
		PrintSNAKE(padding_SNAKE_x,padding_SNAKE_y);
		//绘制菜单栏选项
		int padding_oppration_x = consoleWidth/2-3;
		int padding_oppration_y = consoleHeight/2;
		const char* opp[]={
			"开始游戏(\"1\")",
			"游戏说明(\"2\")",
			"退出游戏(\"Esc\")"
		};
		for (i=0;i<sizeof(opp)/sizeof(opp[0]);i++){
			gotoxy(padding_oppration_x,padding_oppration_y+i*consoleHeight/10);
			printf ("%s",opp[i]);
		}
		//接收菜单栏操作
		int tmp;
		while ( 1 ){
			tmp = getch();
			//Esc，退出游戏
			if ( tmp == 0x1b ){
				Index = -1;
				break;
			}
			//F5,刷新窗口
			if ( tmp == 0x0 ){
				tmp=getch();
				if ( tmp == 0x3f ){
					CreateMenuWindow();
					break;
				}
			}
			//1，2，开始游戏，游戏说明
			if ( tmp == '1' ){
                Index = 1;
                break;
            }
            if ( tmp == '2' ){
                Index = 2;
                break;
            }
		}
			return;
	}


void CreateInstructionWindow(){
        //屏幕初始化
        //清屏
		clrscr();
		//获取屏幕缓冲区信息
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
   		GetConsoleScreenBufferInfo(hConsole, &csbi);
   		//获取控制台宽度、高度
  	  	int consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  	  	int consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
   		//绘制边框，大小为控制台长、宽的4/5，偏移量为1/10
		int padding_width = consoleWidth/10;
		int padding_height = consoleHeight/10;
		//绘制窗口边界
		int i;
		//up
		gotoxy(padding_width,padding_height);
		for (i=padding_width;i<consoleWidth-padding_width;i++){
			putchar('*');
		}
		//down 
		gotoxy(padding_width,consoleHeight-padding_height);
		for (i=padding_width;i<consoleWidth-padding_width;i++){
			putchar('*');
		}
		//left
		for (i=padding_height;i<consoleHeight-padding_height;i++){
			gotoxy(padding_width,i);
			putchar('*');
		}
		//right
		for (i=padding_height;i<consoleHeight-padding_height;i++){
			gotoxy(consoleWidth-padding_width,i);
			putchar('*');
		}
        //绘制左上角信息
        gotoxy(0,0);
        printf ("按下Esc返回主菜单\n按下F5刷新窗口");
        //绘制标题“游戏说明”
        int padding_title_x = consoleWidth/2-4;
        int padding_title_y = consoleHeight/5;
        gotoxy(padding_title_x,padding_title_y);
        printf ("游 戏 说 明");
        //绘制说明内容
        //玩法
        gotoxy(consoleWidth/2-2,padding_title_y+2);
        printf ("玩法说明");
        gotoxy(consoleWidth/2-15,padding_title_y+3);
        printf ("1.使用WASD键控制蛇的移动方向");
        gotoxy(consoleWidth/2-15,padding_title_y+4);
        printf ("2.吃到食物后，蛇的长度会增加");
        gotoxy(consoleWidth/2-15,padding_title_y+5);
        printf ("3.蛇碰到墙壁或自身时，游戏结束");
        gotoxy(consoleWidth/2-15,padding_title_y+6);
        printf ("4.游戏中按Esc暂停，再按Esc返回菜单");
        gotoxy(consoleWidth/2-15,padding_title_y+7);
        printf ("5.按F1加速，按F2减速，高速时得分20，低速时得分10");
        gotoxy(consoleWidth/2-15,padding_title_y+8);
        printf ("注意：请勿在游戏进行过程中拖动窗口");
        //历史最高分
        gotoxy(consoleWidth/2-3,padding_title_y+10);
        printf ("历史最高分");
        gotoxy(consoleWidth/2,padding_title_y+11);
        printf ("%d",ReadMaxScore());
        //特别鸣谢
        gotoxy(consoleWidth/2-2,padding_title_y+13);
        printf ("特别鸣谢");
        gotoxy(consoleWidth/2-15,padding_title_y+14);
        printf ("感谢参与制作的成员：温凯鑫，黄奕铭");
        //接收键盘操作
        int tmp;
        while ( 1 ){
            tmp = getch();
            //特殊键Esc，返回主菜单
            if ( tmp == 0x1b ){
                Index = 0;
                break;
            }
            //特殊键F5,刷新窗口比例
            if ( tmp == 0x0 ){
			tmp=getch();
			if ( tmp == 0x3f ){
				clrscr();
				CreateInstructionWindow();
				break;
			}
		}
        }
        return;
}

void CreatePlayWindow(){
        score = 0;
        //屏幕初始化
		//清屏
		clrscr();
		//获取屏幕缓冲区信息
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
   		GetConsoleScreenBufferInfo(hConsole, &csbi);
   		//获取控制台宽度、高度
  	  	int consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  	  	int consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
   		//绘制边框，大小为控制台长、宽的4/5，偏移量为1/10
		int padding_width = consoleWidth/10;
		int padding_height = consoleHeight/10;
		//绘制窗口边界
		int i;
		//up
		gotoxy(padding_width,padding_height);
		for (i=padding_width;i<consoleWidth-padding_width;i++){
			putchar('*');
		}
		//down 
		gotoxy(padding_width,consoleHeight-padding_height);
		for (i=padding_width;i<consoleWidth-padding_width;i++){
			putchar('*');
		}
		//left
		for (i=padding_height;i<consoleHeight-padding_height;i++){
			gotoxy(padding_width,i);
			putchar('*');
		}
		//right
		for (i=padding_height;i<consoleHeight-padding_height;i++){
			gotoxy(consoleWidth-padding_width,i);
			putchar('*');
		}
		//绘制左上角信息
        //左上角空位大概率2行，把部分内容整合到一行输出起
		//最高分,当前分
		gotoxy(0,0);
		printf ("历史最高分:%d\t\t当前得分:%d",ReadMaxScore(),score);
		//移动操作,刷新操作，暂停操作
        gotoxy(0,1);
		printf ("WSAD移动\tF1加速 F2减速\t游戏开始前Esc返回主菜单,开始后Esc暂停");
        //游戏内容区域
            //设置光标不可见
    CONSOLE_CURSOR_INFO cci;
    cci.dwSize=sizeof(cci);
    cci.bVisible=FALSE;
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cci);
    kbhit();
    //播种随机数
    srand(time(NULL)); 
    initsnake();
    initfood();
    playgame();
    return;
}

int CreatePauseWindow(){
        //CreatePauseWindow函数包含在CreatePlayWindow函数中
        //根据CreatePauseWindow函数的返回值ret在CreatePlayWindow函数中操作Index
        int ret;
        //屏幕初始化
		//清屏
		clrscr();
        //获取屏幕缓冲区信息
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
   		GetConsoleScreenBufferInfo(hConsole, &csbi);
   		//获取控制台宽度、高度
  	  	int consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  	  	int consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
        //绘制边框，大小为控制台长、宽的2/5，偏移量为3/10
        int padding_width = consoleWidth*3/10;
        int padding_height = consoleHeight*3/10;
        //绘制窗口边界
        int i;
        //up
        gotoxy(padding_width,padding_height);
        for (i=padding_width;i<consoleWidth-padding_width;i++){
            putchar('*');
        }
        //down
        gotoxy(padding_width,consoleHeight-padding_height);
        for (i=padding_width;i<=consoleWidth-padding_width;i++){
            putchar('*');
        }
        //left
        for (i=padding_height;i<consoleHeight-padding_height;i++){
            gotoxy(padding_width,i);
            putchar('*');
        }
        //right
        for (i=padding_height;i<consoleHeight-padding_height;i++){
            gotoxy(consoleWidth-padding_width,i);
            putchar('*');
        }
        //绘制左上角信息
        //最高分,当前分
        gotoxy(0,0);
        printf ("历史最高分：%d\t当前得分：%d",ReadMaxScore(),score );
        //WASD移动，F5刷新
        gotoxy(0,1);
        printf ("WASD移动\tF5刷新");
        //绘制窗口内容
        gotoxy(consoleWidth/2-4,consoleHeight*4/10);
        printf ("游 戏 暂 停");
        gotoxy(consoleWidth/2-2,consoleHeight*4/10+2);
        printf ("继续\"1\"");
        gotoxy(consoleWidth/2-3,consoleHeight*4/10+4);
        printf ("重新开始\"2\"");
        gotoxy(consoleWidth/2-3,consoleHeight*4/10+6);
        printf ("结束游戏\"3\"");
        gotoxy(consoleWidth/2-4,consoleHeight*4/10+8);
        printf ("返回主菜单\"Esc\"");
        //接收菜单栏输入
        int tmp;
        while (1){
            tmp = getch();
            //继续
            if (tmp == '1'){ 
                ret = 1;
                break;
            }
            //重新开始
            if (tmp == '2'){
                ret = 2;
                break;
            }
            //结束游戏
            if (tmp == '3'){
                ret = 3;
                break;
            }
            //Esc
            if (tmp == 0x1b){
                ret = 0;
                break;
            }
            //F5
            //注意F5时不要丢失最后一个操作窗口的返回值
            if ( tmp == 0x0 ){
                tmp=getch();
                if (tmp == 0x3f){
                    //保留最后一次操作的函数返回值
                    ret = CreatePauseWindow();
                    break;
                }
            }
        }
        //返回返回值ret，在CreatePlayWindow函数中操作Index
        return ret;
}

void CreateEndWindow(){
        //如果刷新记录，更新最高分
        int is_break = 0;
        if ( score > ReadMaxScore()){
            //更新最高分
            FILE *fp;
            fp = fopen("maxscore.txt", "w");
            if (fp != NULL) {
                fprintf(fp, "%d", score);
                fclose(fp);
            }
            maxScore = score; //更新内存中的最高分
            is_break = 1; //标记需要刷新最高分
        }
        //屏幕初始化
		//清屏
		clrscr();
		//获取屏幕缓冲区信息
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
   		GetConsoleScreenBufferInfo(hConsole, &csbi);
   		//获取控制台宽度、高度
  	  	int consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  	  	int consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
        //绘制边框，大小为控制台长、宽的2/5，偏移量为3/10
        int padding_width = consoleWidth*3/10;
        int padding_height = consoleHeight*3/10;
        //绘制窗口边界
        int i;
        //up
        gotoxy(padding_width,padding_height);
        for (i=padding_width;i<consoleWidth-padding_width;i++){
            putchar('*');
        }
        //down
        gotoxy(padding_width,consoleHeight-padding_height);
        for (i=padding_width;i<=consoleWidth-padding_width;i++){
            putchar('*');
        }
        //left
        for (i=padding_height;i<consoleHeight-padding_height;i++){
            gotoxy(padding_width,i);
            putchar('*');
        }
        //right
        for (i=padding_height;i<consoleHeight-padding_height;i++){
            gotoxy(consoleWidth-padding_width,i);
            putchar('*');
        }
        //绘制左上角信息
        //最高分
        gotoxy(0,0);
        printf ("历史最高分：%d\n",ReadMaxScore() );
        //F5刷新
        printf ("按下F5刷新");
        //绘制窗口内容
        //游戏结束
        gotoxy(consoleWidth/2-4,consoleHeight*2/5);
        printf ("游 戏 结 束");
        //刷新最高分
        if ( is_break ){
            gotoxy(consoleWidth/2-6,consoleHeight/2-1);
            printf ("恭 喜 刷 新 记 录");
        }
        //本次得分
        gotoxy(consoleWidth/2-4,consoleHeight/2);
        printf ("本次得分：%d",score);
        //菜单栏选项
        gotoxy(consoleWidth/2-4,consoleHeight*3/5);
        printf ("重新开始(\"1\")");
        gotoxy(consoleWidth/2-4,consoleHeight*3/5+2);
        printf ("返回主菜单(\"Esc\")");
        //接收菜单栏输入
        int tmp;
        while (1){
            //重新开始
            tmp = getch();
            if (tmp == '1'){ 
                Index = 1;
                break;
            }
            //Esc
            if (tmp == 0x1b){
                Index = 0;
                break;
            }
            //F5
           if ( tmp == 0x0 ){
			tmp=getch();
			if ( tmp == 0x3f ){
				clrscr();
				CreateEndWindow();
				break;
			}
		}
     }
}

void initsnake()
{
	//获取屏幕缓冲区信息
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
   	GetConsoleScreenBufferInfo(hConsole, &csbi);
   	//获取控制台宽度、高度
    int consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  	int consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  	//绘制边框，大小为控制台长、宽的4/5，偏移量为1/10
	int padding_width = consoleWidth/10;
	int padding_height = consoleHeight/10;
    //蛇初始化
    snake.size=2;
    snake.head.x=consoleWidth/2;
    snake.head.y=consoleHeight/2;
    snake.body[0].x=consoleWidth/2-1;
    snake.body[0].y=consoleHeight/2;
}

void initfood()
{
	//获取屏幕缓冲区信息
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
   		GetConsoleScreenBufferInfo(hConsole, &csbi);
   		//获取控制台宽度、高度
  	  	int consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  	  	int consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  		//绘制边框，大小为控制台长、宽的4/5，偏移量为1/10
		int padding_width = consoleWidth/10;
		int padding_height = consoleHeight/10;
    //食物初始化
    Food.x=rand()%(consoleWidth-2*padding_width-1)+padding_width+1;
    Food.y=rand()%(consoleHeight-2*padding_height-1)+padding_height+1;
}

void putsnake()
{
    //修改控制台光标位置
    gotoxy(snake.head.x,snake.head.y);
    //画蛇与食物
    putchar('@');
    int i;
    for(i=0;i<snake.size-1;i++)
    {
        gotoxy(snake.body[i].x,snake.body[i].y);
        putchar('*');
    }
    //去除蛇尾
    gotoxy(lastX,lastY);
    putchar(' ');
    //画食物
    gotoxy(Food.x,Food.y);
    putchar('#');
    gotoxy(WIDTH,HIGHT);
}

void playgame()
{   
	sleepsecond=400;
	//获取屏幕缓冲区信息
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
   	GetConsoleScreenBufferInfo(hConsole, &csbi);
   	//获取控制台宽度、高度
  	int consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  	int consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  	//绘制边框，大小为控制台长、宽的4/5，偏移量为1/10
	int padding_width = consoleWidth/10;
	int padding_height = consoleHeight/10;
    //画蛇头与食物
	gotoxy(snake.head.x,snake.head.y);
    putchar('@');
    gotoxy(Food.x,Food.y);
    putchar('#');
    gotoxy(WIDTH,HIGHT);
    //判断第一次输入是WASD
    char key;
    while ( 1 ){
        key = getch();
        if (key == 'w' || key == 's' || key == 'a' || key == 'd' ) {
            break; // 如果是WASD，跳出循环
        }
        if ( key == 0x1b ){
            Index = 0;
            return; // 如果是Esc，返回主菜单
        }
    }
    //判断蛇撞墙
    while(snake.head.x>=padding_width+1 && snake.head.x<consoleWidth-padding_width-1
        &&snake.head.y>=padding_height+1 && snake.head.y<consoleHeight-padding_height)
        {
            //清屏
            //system("cls"); 
            //接受用户输入
            char lastkey;
            if( kbhit() )
            {
            	lastkey=key;
                key=getch();
            }
            //输入F1，F2
            if(key==0x0)
            {
                key=getch();
                switch (key)
                {
                case 0x3b:
                //F1加速
                    if(sleepsecond>50)
                    {
                        sleepsecond-=30;
                    }
                    break;
                case 0x3c:
                //F2减速
                    if(sleepsecond<500)
                    {
                        sleepsecond+=30;
                    }
                    break;
                default:
                    break;
                }
            }
            //输入WASD
            else
            {   int ret;
                switch (key)
            {
            case 'w':
                if(ky!=1){
                kx=0;ky=-1;}
                break;
            case 's':
                if(ky!=-1){
                kx=0;ky=1;}
                break;
            case 'a':
                if(kx!=1){
                kx=-1;ky=0;}
                break;
            case 'd':
                if(kx!=-1){
                kx=1;ky=0;}
                break;
            case 0x1b:
                //Esc暂停
                ret=CreatePauseWindow();
                //继续
                if(ret==1){
                //屏幕初始化
                //清屏
                clrscr();
		    	//绘制窗口边界
		    	int i;
		    	//up
		    	gotoxy(padding_width,padding_height);
		    	for (i=padding_width;i<consoleWidth-padding_width;i++){
				putchar('*');
		    	}
		    	//down 
		    	gotoxy(padding_width,consoleHeight-padding_height);
		    	for (i=padding_width;i<consoleWidth-padding_width;i++){
		    		putchar('*');
		    	}
		    	//left
		    	for (i=padding_height;i<consoleHeight-padding_height;i++){
		    		gotoxy(padding_width,i);
		    		putchar('*');
		    	}
			    //right
			    for (i=padding_height;i<consoleHeight-padding_height;i++){
			    	gotoxy(consoleWidth-padding_width,i);
			    	putchar('*');
			    }
	    		//绘制左上角信息
             	//左上角空位大概率2行，把部分内容整合到一行输出起
	    		//最高分,当前分
		    	gotoxy(0,0);
		    	printf ("历史最高分:%d\t\t当前得分:%d",ReadMaxScore(),score );
		    	//移动操作,刷新操作，暂停操作
                gotoxy(0,1);
		    	printf ("WSAD移动\tF1加速 F2减速\t游戏开始前Esc返回主菜单,开始后Esc暂停");
                //保留暂停前的输入值
		    	key=lastkey;
			    }

			    else if(ret==2)
			    {
			    	//重新开始
                    Index=1;
			    	return;
    			}
    			else if(ret==3)
    			{
                    //结束游戏
                    Index=3;
	    			return;
	    		}
	    		else if(ret==0)
	    		{
	    			//返回主菜单
                    Index=0;
	    			return;
	    		}
	    			break;
                default:
                    break;
                }
        }
            //蛇撞身体
            int i;
            for(i=0;i<snake.size-1;i++)
            {
                if(snake.head.x==snake.body[i].x
                    &&snake.head.y==snake.body[i].y)
                    {
                        Index=3;
                        return;
                    }
            }
            //蛇吃食物
            if(snake.head.x==Food.x&&snake.head.y==Food.y)
            {
                //食物消失
                initfood();
                //身体增长
                snake.size++;
                //加分
                if (sleepsecond<100)
                {
                    score+=20;
                }
                else
                {
                    score+=10;
                }
                //加速
                if (sleepsecond>50)
                {
                    sleepsecond-=10;
                }
            }
            //蛇移动
            //存储蛇尾坐标
            lastX=snake.body[snake.size-2].x;
            lastY=snake.body[snake.size-2].y;
            for(i=snake.size-1;i>0;i--)
            {
                snake.body[i].x=snake.body[i-1].x;
                snake.body[i].y=snake.body[i-1].y;
            }
            snake.body[0].x=snake.head.x;
            snake.body[0].y=snake.head.y;
            snake.head.x+=kx;
            snake.head.y+=ky;
            //更新蛇
            putsnake();
            gotoxy(0,0);
		    printf ("历史最高分:%d\t\t当前得分:%d",ReadMaxScore(),score);
            //putwall();
            Sleep(sleepsecond); 
        }
        Index = 3; // 游戏结束时设置Index为3，表示进入结束窗口
        return;
}